package org.example._07aop.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

/**
 * Aspect注解：标识此类为切面类;
 */
@Aspect
@Component
public class LogAspect {

    /**
     * Before注解：
     * 1.标识此方法是一个前置通知;
     * 2.调用目标方法之前执行的方法;
     */
    @Before(value = "execution(public int org.example._07aop.aop.CalculatorImpl(..))")
    public void before(JoinPoint joinPoint) {
        String name = joinPoint.getSignature().getName();
        System.out.println("方法开始执行,参数为:" + name);
    }

    @AfterReturning(value = "execution(public int org.example._07aop.aop.CalculatorImpl(..))", returning = "result")
    public void aft(JoinPoint joinPoint, Object result) {

    }

    @AfterThrowing(value = "execution(public int org.example._07aop.aop.CalculatorImpl(..))", throwing = "e")
    public void afterThrowing(JoinPoint joinPoint, Exception e) {
        String name = joinPoint.getSignature().getName();
        System.out.println("异常通知" + name + "信息为" + e.getMessage());
    }

    @Around(value = "execution(public int  org.example._07aop.aop.CalculatorImpl(..))")
    public Object around(ProceedingJoinPoint joinPoint) {
        Object result = null;
        System.out.println("环绕通知:前置通知 ");
        try {
            result = joinPoint.proceed();
            System.out.printf("环绕通知:返回通知"+result);
        } catch (Throwable e) {
            System.out.printf("环绕通知:异常通知"+e );
            throw new RuntimeException(e);
        }finally {
            System.out.printf("环绕通知:后置通知");
        }
        return result;
    }
}

















